package com.yunatt.service.impl;

import com.yunatt.mapper.DeviceMapper;
import com.yunatt.mapper.MachineCommandMapper;
import com.yunatt.model.DeviceEntity;
import com.yunatt.model.MachineCommandEntity;
import com.yunatt.model.vo.LockGroup;
import com.yunatt.model.vo.UserLock;
import com.yunatt.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private MachineCommandMapper machineCommandMapper;

    @Override
    public List<DeviceEntity> findAllDevice() {
        // TODO Auto-generated method stub
        List<DeviceEntity>deviceLists=deviceMapper.findAllDevice();
        return deviceLists;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return deviceMapper.deleteById(id);
    }

    @Override
    public int insert(String serialNum, int status) {
        // TODO Auto-generated method stub
        DeviceEntity device = new DeviceEntity();
        device.setStatus(status);
        device.setSerialNum(serialNum);
        int i=deviceMapper.insert(device);
        return i;
    }



    @Override
    public DeviceEntity selectDeviceBySerialNum(String serialNum) {
        // TODO Auto-generated method stub
        DeviceEntity device=deviceMapper.selectDeviceBySerialNum(serialNum);
        return device;
    }

    @Override
    public int updateStatusByPrimaryKey(int id, int status) {
        // TODO Auto-generated method stub
        return deviceMapper.updateStatusByPrimaryKey(id, status);
    }

    @Override
    public List<DeviceEntity> selectAllDevice() {
        // TODO Auto-generated method stub
        return deviceMapper.findAllDevice();
    }

    @Override
    public void setLockGroup(LockGroup lockGroup) {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"cmd\":\"setdevlock\",\"lockgroup\":[");
        if (lockGroup.getGroup1().equals("")) {
            lockGroup.setGroup1("0");
        }
        if (lockGroup.getGroup2().equals("")) {
            lockGroup.setGroup2("0");
        }
        if (lockGroup.getGroup3().equals("")) {
            lockGroup.setGroup3("0");
        }
        if (lockGroup.getGroup4().equals("")) {
            lockGroup.setGroup4("0");
        }
        if (lockGroup.getGroup5().equals("")) {
            lockGroup.setGroup5("0");
        }
        sb.append("{\"group\":" + lockGroup.getGroup1() + "},");
        sb.append("{\"group\":" + lockGroup.getGroup2() + "},");
        sb.append("{\"group\":" + lockGroup.getGroup3() + "},");
        sb.append("{\"group\":" + lockGroup.getGroup4() + "},");
        sb.append("{\"group\":" + lockGroup.getGroup5() + "}]}");

        String message = sb.toString();
        List<DeviceEntity> deviceList = deviceMapper.findAllDevice();
        for (int i = 0; i < deviceList.size(); i++) {

            MachineCommandEntity machineCommand = new MachineCommandEntity();
            machineCommand.setContent(message);
            machineCommand.setName("setdevlock");
            machineCommand.setStatus(0);
            machineCommand.setSendStatus(0);
            machineCommand.setErrCount(0);
            machineCommand.setSerial(deviceList.get(i).getSerialNum());
            machineCommand.setGmtCrate(new Date());
            machineCommand.setGmtModified(new Date());

            // machineComandService.addMachineCommand(machineCommand);
            machineCommandMapper.insert(machineCommand);
        }
    }

    @Override
    public void setUserLock(UserLock userLock, String starTime, String endTime) {
        StringBuilder sb=new StringBuilder();
        sb.append("{\"cmd\":\"setuserlock\",\"count\":1,\"record\":[{");

        int weekZone2=2;
        sb.append("\"enrollid\":" +userLock.getEnrollId()+ ",");
        sb.append("\"weekzone\":" +userLock.getWeekZone()+ ",");

        sb.append("\"group\":" +userLock.getGroup()+ ",");
        sb.append("\"starttime\":\""+starTime+" 00:00:00"+"\",");
        sb.append("\"endtime\":\""+endTime+" 00:00:00"+"\"}]}");
        String message=sb.toString();
        List<DeviceEntity>deviceList=deviceMapper.findAllDevice();
        for (int i = 0; i < deviceList.size(); i++) {

            MachineCommandEntity machineCommand=new MachineCommandEntity();
            machineCommand.setContent(message);
            machineCommand.setName("setuserlock");
            machineCommand.setStatus(0);
            machineCommand.setSendStatus(0);
            machineCommand.setErrCount(0);
            machineCommand.setSerial(deviceList.get(i).getSerialNum());
            machineCommand.setGmtCrate(new Date());
            machineCommand.setGmtModified(new Date());
            machineCommandMapper.insert(machineCommand);
        }
    }
}
